package org.icosagon.ui;

import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentListener;

/**
 * This class consists of two JLabels  or a JLabel/JTextField that make up a key-value
 * pair to display to the user.
 */
public class LabeledValue extends JComponent {
    private JLabel key = new JLabel("");
    private JTextField data = new JTextField();

    public LabeledValue(String label, String value) {
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        Font font=new Font(this.key.getFont().getName(),Font.BOLD,this.key.getFont().getSize());
        this.key.setFont(font);

        this.data.setBorder(null);

        this.setLabelText(label);
        this.setValueText(value);

        this.add(this.key);
        this.add(this.data);
     }

    public void setLabelText(String value) {
        if(value == null)
            value = "";
        this.key.setText(value+":");
    }

    public String getLabelText() {
        return this.key.getText();
    }

    public void setEditable(boolean value) {
        this.data.setEditable(value);
    }

    public boolean isEditable() {
        return this.data.isEditable();
    }

    public void setValueText(String value) {
        if(value == null)
            value = "";
        this.data.setText(value);
    }

    public String getValueText() {
        return this.data.getText();
    }

    public void addDocumentListener(DocumentListener listn) {
        this.data.getDocument().addDocumentListener(listn);
    }

    public void removeDocumentListener(DocumentListener listn) {
        this.data.getDocument().removeDocumentListener(listn);
    }
}